package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.FunctionsMapper;
import com.shaobufan.museum.pojo.entity.Functions;
import com.shaobufan.museum.pojo.vo.FunctionsVo;
import com.shaobufan.museum.service.DepartmentService;
import com.shaobufan.museum.service.FunctionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * @description 针对表【functions(功能表)】的数据库操作Service实现
 * @createDate 2024-04-22 13:42:29
 */
@Service
public class FunctionsServiceImpl extends ServiceImpl<FunctionsMapper, Functions>
        implements FunctionsService {

    @Resource
    FunctionsMapper functionsMapper;

    @Resource
    DepartmentService departmentService;

    /**
     * 搜索所有功能
     *
     * @return List Functions
     */
    @Override
    public List<FunctionsVo> searchAllFunctions() {
        return functionsMapper.selectList(null).stream().map(this::getFunctionsVo).collect(Collectors.toList());
    }

    /**
     * 根据部门查询功能
     *
     * @param departId 部门id
     * @return List Functions
     */
    @Override
    public List<FunctionsVo> searchFunctionsByDepartId(Integer departId) {
        QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("depart_id", departId);
        return functionsMapper.selectList(queryWrapper).stream().map(this::getFunctionsVo).collect(Collectors.toList());
    }

    @Override
    public Boolean alterFunctions(Integer functionId, Integer departId) {
        QueryWrapper<Functions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("function_id", functionId);
        Functions functions = new Functions();
        functions.setDepartId(departId);
        functionsMapper.update(functions, queryWrapper);
        return true;
    }

    public FunctionsVo getFunctionsVo(Functions functions) {
        FunctionsVo functionsVo = new FunctionsVo();
        functionsVo.setFunctionId(functions.getFunctionId());
        functionsVo.setFunctionName(functions.getFunctionName());
        functionsVo.setFunctionIndex(functions.getFunctionIndex());
        functionsVo.setDepartId(functions.getDepartId());
        functionsVo.setDepartName(departmentService.searchDepartmentById(functions.getDepartId()).getDepartDetail());
        return functionsVo;
    }
}